﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MyWeb.Data;
using MyWeb.Business;
using MyWeb.Common;
using System.IO;

namespace MyWeb.Admin
{
	public partial class Module : System.Web.UI.Page
	{
		static string Level = "";
		static string Lang = "";
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				lbtDeleteT.Attributes.Add("onClick", "javascript:return confirm('Bạn có muốn xóa?');");
				lbtDeleteB.Attributes.Add("onClick", "javascript:return confirm('Bạn có muốn xóa?');");
				NumberClass.OnlyInputNumber(txtOrd);
				Lang = Session["Lang"].ToString();
				BindGrid();
                LoadImageDropDownList();
			}
            for (int i = 0; i <= ddlImage.Items.Count - 1; i++)
            {
                ddlImage.Items[i].Attributes.Add("title", ddlImage.Items[i].Value);
            }
		}

		private void BindGrid()
		{
			grdModule.DataSource = ModuleService.Module_GetByAll();
			grdModule.DataBind();
			if (grdModule.PageCount <= 1)
			{
				grdModule.PagerStyle.Visible = false;
			}
		}

        private void LoadImageDropDownList()
        {
            ddlImage.Items.Clear();
            ddlImage.Items.Add(new ListItem(" -- Chọn icon -- ", ""));
            DirectoryInfo strPath = new DirectoryInfo(GlobalClass.PhysicalApplicationPath(GlobalClass.GetUrlAdminImage()));
            if (strPath.Exists)
            {
                FileInfo[] file = strPath.GetFiles("icon_*");
                for (int i = 0; i < file.Length; i++)
                {
                    string strName = StringClass.GetFileName(file[i].Name);
                    string strImage = GlobalClass.GetUrlAdminImage() + file[i].Name;
                    ListItem item = new ListItem("File " + strName, strImage);
                    ddlImage.Items.Add(item);
                }
            }
        }

		protected void grdModule_ItemDataBound(object sender, DataGridItemEventArgs e)
		{
			ListItemType itemType = e.Item.ItemType;
			if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
			{
				if (itemType == ListItemType.Header)
				{
					object checkBox = e.Item.FindControl("chkSelectAll");
					if ((checkBox != null))
					{
						((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
					}
				}
				else
				{
					string tableRowId = grdModule.ClientID + "_row" + e.Item.ItemIndex.ToString();
					e.Item.Attributes.Add("id", tableRowId);
					object checkBox = e.Item.FindControl("chkSelect");
					if ((checkBox != null))
					{
						e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
						e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex.ToString() + ")");
						((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex.ToString() + ")");
					}
				}
			}
		}

		protected void grdModule_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
		{
			grdModule.CurrentPageIndex = e.NewPageIndex;
			BindGrid();
		}

		protected void grdModule_ItemCommand(object source, DataGridCommandEventArgs e)
		{
			string strCA = e.CommandArgument.ToString();
			switch (e.CommandName)
			{
				case "AddSub":
					Level = strCA;
					AddButton_Click(source, e);
					break;
				case "Edit":
					txtId.Value = strCA;
                    List<Data.Module> listE = ModuleService.Module_GetById(strCA);
					Level = listE[0].Level.Substring(0, listE[0].Level.Length - 5);
					txtCode.Text = listE[0].Code;
					txtName.Text = listE[0].Name;
					txtPage.Text = listE[0].Page;
					ddlImage.Text = listE[0].Image;
					txtOrd.Text = listE[0].Ord;
					pnView.Visible = false;
					pnUpdate.Visible = true;
					break;
				case "Delete":
					ModuleService.Module_Delete(strCA);
					BindGrid();
					break;
			}
		}

		protected void AddButton_Click(object sender, EventArgs e)
		{
			pnUpdate.Visible = true;
			ControlClass.ResetControlValues(this);
			pnView.Visible = false;
		}

		protected void DeleteButton_Click(object sender, EventArgs e)
		{
			DataGridItem item = default(DataGridItem);
			for (int i = 0; i < grdModule.Items.Count; i++)
			{
				item = grdModule.Items[i];
				if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
				{
					if (((CheckBox)item.FindControl("ChkSelect")).Checked)
					{
						string strId = item.Cells[1].Text;
						ModuleService.Module_Delete(strId);
					}
				}
			}
			grdModule.CurrentPageIndex = 0;
			BindGrid();
		}

		protected void RefreshButton_Click(object sender, EventArgs e)
		{
			BindGrid();
		}

		protected void Update_Click(object sender, EventArgs e)
		{
			if (Page.IsValid){
				Data.Module obj = new Data.Module();
                obj.Id = txtId.Value;
				obj.Code = txtCode.Text;
				obj.Level = Level + "00000";
				obj.Name = txtName.Text;
				obj.Page = txtPage.Text;
                obj.Image = ddlImage.SelectedValue;
				obj.Ord = txtOrd.Text != "" ? txtOrd.Text : "1";
				ModuleService.Module_Update(obj);
				BindGrid();
				Level= "";
				pnView.Visible = true;
				pnUpdate.Visible = false;
			}
		}

		protected void Back_Click(object sender, EventArgs e)
		{
			Level= "";
			pnView.Visible = true;
			pnUpdate.Visible = false;
		}
	}
}